{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pptwinpics.oss-cn-beijing.aliyuncs.com/CDA%E8%AE%B2%E5%B8%88%E6%B0%B4%E5%8D%B0_20200314161940.png)\n",
    "# 前言\n",
    "\n",
    " 大家好，我是 CDA 曹鑫。\n",
    "\n",
    "**课程代码请访问我的 Github：**[Github 项目库](https://github.com/imcda/Python-Tutorial-for-Humans/blob/master/001-Python%20%E6%95%99%E7%A8%8B%E7%AE%80%E4%BB%8B/002-%E8%A7%81%E8%AF%86%E4%B8%80%E4%B8%8B%20Python.ipynb)\n",
    "\n",
    " **学数据科学，上 [CDA.cn](http://cda.cn)**\n",
    "\n",
    "用 Jupyter Notebook 来教 Jupyter Notebook 的用法，才是正确的打开方式吧？\n",
    "\n",
    "# 什么是Jupyter Notebook？\n",
    "\n",
    "Jupyter Notebook 是以网页的形式打开，可以在网页页面中直接编写代码和运行代码，代码的运行结果也会直接在代码块下显示的程序。如在编程过程中需要编写说明文档，可在同一个页面中直接编写，便于作及时的说明和解释。说的再多，不如亲手试试。\n",
    "\n",
    "那怎么打开 Jupyter Notebook 呢？上篇教程[Anaconda 安装及操作](https://github.com/imcda/Python-Tutorial-for-Humans/blob/master/002-%E5%AE%89%E8%A3%85%20Python/001-Anaconda%20%E5%AE%89%E8%A3%85%E5%8F%8A%E6%93%8D%E4%BD%9C.md)我们已经成功安装 Anaconda，现在打开 Anaconda-Navigator ，就是这个：\n",
    "\n",
    "![Anaconda-安装-4](https://pptwinpics.oss-cn-beijing.aliyuncs.com/Anaconda-%E5%AE%89%E8%A3%85-4_20191216091111.jpg)\n",
    "\n",
    "启动后，看到的界面如下：\n",
    "\n",
    "![Anaconda-安装-5](https://pptwinpics.oss-cn-beijing.aliyuncs.com/Anaconda-%E5%AE%89%E8%A3%85-5_20191216091254.jpg)\n",
    "\n",
    "选择 Jupyter Notebook 后，在终端中将会显示一系列 notebook 的服务器信息，同时浏览器将会自动启动 Jupyter Notebook。\n",
    "\n",
    "## 启动\n",
    "\n",
    "启动过程中终端显示内容如下：\n",
    "\n",
    "```\n",
    "$ jupyter notebook\n",
    "[I 08:58:24.417 NotebookApp] Serving notebooks from local directory: /Users/catherine\n",
    "[I 08:58:24.417 NotebookApp] 0 active kernels\n",
    "[I 08:58:24.417 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/\n",
    "[I 08:58:24.417 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\n",
    "```\n",
    "\n",
    "**注意：**\n",
    "之后在Jupyter Notebook的所有操作，都请保持终端不要关闭，因为一旦关闭终端，就会断开与本地服务器的链接，你将无法在Jupyter Notebook中进行其他操作啦。\n",
    "浏览器地址栏中默认地将会显示：`http://localhost:8888`。其中，`localhost`指的是本机，`8888`则是端口号。就会自动打开浏览器里面的一个页面，如下：\n",
    "\n",
    "![jupyter-notebook-教程-0](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E6%95%99%E7%A8%8B-0_20191216110146.jpg)\n",
    "\n",
    "如果你同时启动了多个Jupyter Notebook，由于默认端口“8888”被占用，因此地址栏中的数字将从“8888”起，每多启动一个Jupyter Notebook数字就加1，如“8889”、“8890”……\n",
    "\n",
    "\n",
    "## 主页面\n",
    "\n",
    "![jupyter-notebook-教程-1](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E6%95%99%E7%A8%8B-1_20191216105430.jpg)\n",
    "\n",
    "我们简单看一下上面的区域。\n",
    "\n",
    "显示肯定各有不同，不要担心，你仔细看看，就能发现这就是你电脑里面的文件夹和文件。\n",
    "\n",
    "你可以在终端中执行以下2步来查看：\n",
    "\n",
    "**第一步：**\n",
    "`cd` 或 `cd -` 或 `cd ~` 或`cd /Users/<user_name>`\n",
    "\n",
    "这个命令将会进入你的家目录。\n",
    "`<user_name>` 是用户名。用户名两边不加尖括号`<>`。\n",
    "\n",
    "**第二步：**\n",
    "`ls`\n",
    "\n",
    "这个命令将会展示你家目录下的文件。\n",
    "\n",
    "# Jupyter Notebook的基本使用\n",
    "\n",
    "重点就是下面的编辑区域，希望你已经打开 Jupyter Notebook 跟我一起开始操作了。\n",
    "\n",
    "## 文件（Files）页面\n",
    "\n",
    "![jupyter-notebook-files页面](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-files%E9%A1%B5%E9%9D%A2_20191216180655.jpg)\n",
    "\n",
    "此时你的界面当中应该还没有`Conda`和`Nbextensions`类目。不要着急，这两个类目将分别在“五、拓展功能”中的“1.关联Jupyter Notebook和conda的环境和包——‘nb_conda’”和“2.Markdown生成目录”中安装。\n",
    "\n",
    "`文件（Files）`页面是用于管理和创建文件相关的类目。\n",
    "\n",
    "对于现有的文件，可以通过勾选文件的方式，对选中文件进行复制、重命名、移动、下载、查看、编辑和删除的操作。\n",
    "\n",
    "同时，也可以根据需要，在`新建（New）`下拉列表中选择想要创建文件的环境，进行创建\n",
    "+ `ipynb`格式的笔记本，即选择对应 Python 版本创建\n",
    "+ `txt`格式的文档\n",
    "+ 终端\n",
    "+ 文件夹。\n",
    "\n",
    "### 笔记本的基本操作\n",
    "\n",
    "![jupyter-notebook-笔记本页面注解图](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E7%AC%94%E8%AE%B0%E6%9C%AC%E9%A1%B5%E9%9D%A2%E6%B3%A8%E8%A7%A3%E5%9B%BE_20191216180440.jpg)\n",
    "\n",
    "上图展示的是笔记本的基本结构和功能。根据图中的注解已经可以解决绝大多数的使用问题了！\n",
    "\n",
    "工具栏的使用如图中的注解一样直观，在此不过多解释。需要特别说明的是\n",
    "\n",
    "**单元格的状态**，有\n",
    "- `代码（Code）`：即我们编写 Python 代码的区域\n",
    "- `笔记（Markdown）`：通过 Markdown 语法来记录笔记，正如我现在写的内容，语法也非常简单，多用几次就记住了，是非常实用的技能，推荐你学习一下[《Markdown Tutorial for Humans™》](https://github.com/imcda/Python-Tutorial-for-Humans/blob/master/001-Python%20%E6%95%99%E7%A8%8B%E7%AE%80%E4%BB%8B/004-Markdown%20Tutorial%20for%20Humans%E2%84%A2.md)\n",
    "- `Raw NBconvert`\n",
    "- ~`标题（Heading）`~\n",
    "\n",
    "其中，最常用的是前两个，分别是`代码状态`，`Markdown`编写状态。\n",
    "\n",
    "Jupyter Notebook已经取消了`标题（Heading）`状态，即标题单元格。取而代之的是Markdown的一级至六级标题。而`Raw NBconvert`目前极少用到，此处也不做过多讲解。\n",
    "\n",
    "菜单栏涵盖了笔记本的所有功能，即便是工具栏的功能，也都可以在菜单栏的类目里找到。然而，并不是所有功能都是常用的，比如`Widgets`，`Navigate`。\n",
    "\n",
    "`Kernel`类目的使用，主要是对内核的操作，比如中断、重启、连接、关闭、切换内核等，由于我们在创建笔记本时已经选择了内核，因此切换内核的操作便于我们在使用笔记本时切换到我们想要的内核环境中去。由于其他的功能相对比较常规，根据图中的注解来尝试使用笔记本的功能已经非常便捷，因此不再做详细讲解。\n",
    "\n",
    "### 笔记本重命名的两种方式\n",
    "\n",
    "**笔记本内部重命名**\n",
    "在使用笔记本时，可以直接在其内部进行重命名。在左上方“Jupyter”的图标旁有程序默认的标题`未命名（Untitled）`，点击`未命名（Untitled）`然后在弹出的对话框中输入自拟的标题，点击`重命名（Rename）`即完成了重命名。\n",
    "\n",
    "**笔记本外部重命名**\n",
    "若在使用笔记本时忘记了重命名，且已经保存并退出至`文件（Files）`界面，则在`文件（Files）`界面勾选需要重命名的文件，点击`重命名（Rename）`然后直接输入自拟的标题即可。\n",
    "\n",
    "**演示**\n",
    "![jupyter-notebook-笔记本重命名演示图](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E7%AC%94%E8%AE%B0%E6%9C%AC%E9%87%8D%E5%91%BD%E5%90%8D%E6%BC%94%E7%A4%BA%E5%9B%BE_20191216172915.webp)\n",
    "\n",
    "## 运行（Running）页面\n",
    "\n",
    "`运行（Running）`页面主要展示的是当前正在运行当中的终端和`ipynb`格式的笔记本。若想要关闭已经打开的终端和`ipynb`格式的笔记本，仅仅关闭其页面是无法彻底退出程序的，需要在Running页面点击其对应的`关闭（Shutdown）`。更多关闭方法可以查阅“八、关闭和退出”中的“1.关闭笔记本和终端”。\n",
    "\n",
    "![jupyter-notebook-Running页面功能演示图](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Running%E9%A1%B5%E9%9D%A2%E5%8A%9F%E8%83%BD%E6%BC%94%E7%A4%BA%E5%9B%BE_20191216173024.webp)\n",
    "\n",
    "## 集群（Clusters）页面\n",
    "> Clusters tab is now provided by IPython parallel. See 'IPython parallel' for installation details.\n",
    "\n",
    "Clusters类目现在已由IPython parallel对接，且由于现阶段使用频率较低，因此在此不做详细说明，想要了解更多可以访问[IPython parallel的官方网站](https://github.com/ipython/ipyparallel)。\n",
    "\n",
    "## Conda页面\n",
    "Conda页面主要是Jupyter Notebook与Conda关联之后对Conda环境和包进行直接操作和管理的页面工具。\n",
    "\n",
    "关联Jupyter Notebook和conda的环境和包——`nb_conda`\n",
    "\n",
    "**安装**\n",
    "```\n",
    "conda install nb_conda\n",
    "```\n",
    "执行上述命令能够将你conda创建的环境与Jupyter Notebook相关联，便于你在Jupyter Notebook的使用中，在不同的环境下创建笔记本进行工作。\n",
    "\n",
    "**使用**\n",
    "\n",
    "可以在Conda类目下对conda环境和包进行一系列操作。\n",
    "\n",
    "![jupyter-notebook-Conda页面注解图](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Conda%E9%A1%B5%E9%9D%A2%E6%B3%A8%E8%A7%A3%E5%9B%BE_20191216181820.jpg)\n",
    "\n",
    "可以在笔记本内的`服务（Kernel）`类目里的`改变服务（Change\n",
    "kernel）`切换内核。\n",
    "\n",
    "![jupyter-notebook-服务kernel](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E6%9C%8D%E5%8A%A1kernel_20191216182136.jpg)\n",
    "\n",
    "\n",
    "**卸载**\n",
    "```\n",
    "canda remove nb_conda\n",
    "```\n",
    "执行上述命令即可卸载`nb_conda`包。\n",
    "\n",
    "## 插件（Nbextensions）页面\n",
    "Nbextensions 页面提供了多个 Jupyter Notebook 的插件，使其功能更加强大。该页面中主要使用的插件有`nb_conda`，~`nb_present`~，`Table of Contents(2)`。这些功能我们无需完全掌握，也无需安装所有的扩展功能。\n",
    "\n",
    "![jupyter-notebook-Nbextensions页面](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Nbextensions%E9%A1%B5%E9%9D%A2_20191216183220.jpg)\n",
    "\n",
    "我们以为 Markdown 生成目录为例，来练习一下。根据本文档提供的学习思路，我们只需要安装 `Talbe of Contents(2)` 即可，该功能可为 Markdown 文档提供目录导航，便于我们编写文档。\n",
    "\n",
    "不同于有道云笔记的Markdown编译器，Jupyter Notebook 无法为 Markdown 文档通过特定语法添加目录，因此需要通过安装扩展来实现目录的添加。\n",
    "\n",
    "```\n",
    "conda install -c conda-forge jupyter_contrib_nbextensions\n",
    "```\n",
    "\n",
    "执行上述命令后，启动 Jupyter Notebook，你会发现导航栏多了`Nbextensions`的类目，点击`Nbextensions`，勾选`Table\n",
    "of Contents ⑵`\n",
    "\n",
    "![jupyter-notebook-Nbextensions 页面](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Nbextensions%E9%A1%B5%E9%9D%A2_20191216183220.jpg)\n",
    "\n",
    "之后再在 Jupyter Notebook 中使用 Markdown，点击下图的图标即可使用啦。\n",
    "\n",
    "![jupyter-notebook-Markdown目录](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Markdown%E7%9B%AE%E5%BD%95_20191216183303.jpg)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# 关闭和退出\n",
    "\n",
    "## 关闭笔记本和终端\n",
    "当我们在 Jupyter Notebook 中创建了终端或笔记本时，将会弹出新的窗口来运行终端或笔记本。当我们使用完毕想要退出终端或笔记本时，仅仅关闭页面是无法结束程序运行的，因此我们需要通过以下步骤将其完全关闭。\n",
    "\n",
    "## 方法一\n",
    "a. 进入`Files`页面。\n",
    "b. 勾选想要关闭的`ipynb`笔记本。正在运行的笔记本其图标为绿色，且后边标有`Running`的字样；已经关闭的笔记本其图标为灰色。\n",
    "c. 点击上方的黄色的`Shutdown`按钮。\n",
    "d. 成功关闭笔记本。\n",
    "\n",
    "**注意：**此方法只能关闭笔记本，无法关闭终端。\n",
    "\n",
    "## 方法二\n",
    "a. 进入“Running”页面。\n",
    "b. 第一栏是“Terminals”，即所有正在运行的终端均会在此显示；第二栏是“Notebooks”，即所有正在运行的“ipynb”笔记本均会在此显示。\n",
    "c. 点击想要关闭的终端或笔记本后黄色“Shutdown”按钮。\n",
    "d. 成功关闭终端或笔记本。\n",
    "\n",
    "**注意：**此方法可以关闭任何正在运行的终端和笔记本。\n",
    "\n",
    "**注意**\n",
    "+ 只有`ipynb`笔记本和终端需要通过上述方法才能使其结束运行。\n",
    "+ `txt`文档，即`New`下拉列表中的`Text\n",
    "File`，以及`Folder`只要关闭程序运行的页面即结束运行，无需通过上述步骤关闭。\n",
    "\n",
    "## 演示\n",
    "\n",
    "![jupyter-notebook-关闭笔记本或终端程序](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E5%85%B3%E9%97%AD%E7%AC%94%E8%AE%B0%E6%9C%AC%E6%88%96%E7%BB%88%E7%AB%AF%E7%A8%8B%E5%BA%8F_20191216184516.webp)\n",
    "\n",
    "# 退出Jupyter Notebook程序\n",
    "如果你想退出Jupyter Notebook程序，仅仅通过关闭网页是无法退出的，因为当你打开Jupyter Notebook时，其实是启动了它的服务器。\n",
    "\n",
    "你可以尝试关闭页面，并打开新的浏览器页面，把之前的地址输进地址栏，然后跳转页面，你会发现再次进入了刚才“关闭”的Jupyter Notebook页面。\n",
    "\n",
    "如果你忘记了刚才关闭的页面地址，可以在启动Jupyter Notebook的终端中找到地址，复制并粘贴至新的浏览器页面的地址栏，会发现同样能够进入刚才关闭的页面。\n",
    "\n",
    "因此，想要彻底退出Jupyter Notebook，需要关闭它的服务器。只需要在它启动的终端上按：\n",
    "\n",
    "+ Mac用户：`control c`\n",
    "+ Windows用户：`ctrl c`\n",
    "\n",
    "然后在终端上会提示：`Shutdown this notebook server (y/[n])?`输入`y`即可关闭服务器，这才是彻底退出了 Jupyter Notebook 程序。此时，如果你想要通过输入刚才关闭网页的网址进行访问Jupyter Notebook便会看到报错页面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 操作栏目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pptwinpics.oss-cn-beijing.aliyuncs.com/CDA%E8%AE%B2%E5%B8%88%E6%B0%B4%E5%8D%B0_20200314161940.png)\n",
    "# 技能福利\n",
    "\n",
    "##  加载指定网页源代码\n",
    "\n",
    "**使用场景**\n",
    "想要在Jupyter Notebook中直接加载指定网站的源代码到笔记本中。\n",
    "\n",
    "**方法**\n",
    "执行以下命令:\n",
    "```\n",
    "%load URL\n",
    "```\n",
    "其中，URL为指定网站的地址。\n",
    "\n",
    "**例**\n",
    "![jupyter-notebook-加载网络代码](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E5%8A%A0%E8%BD%BD%E7%BD%91%E7%BB%9C%E4%BB%A3%E7%A0%81_20191216212436.webp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-1ebe32e0eaa4>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-2-1ebe32e0eaa4>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m    <html>\u001b[0m\n\u001b[0m    ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "# %load https://www.baidu.com/\n",
    "<html>\n",
    "<head>\n",
    "\t<script>\n",
    "\t\tlocation.replace(location.href.replace(\"https://\",\"http://\"));\n",
    "\t</script>\n",
    "</head>\n",
    "<body>\n",
    "\t<noscript><meta http-equiv=\"refresh\" content=\"0;url=http://www.baidu.com/\"></noscript>\n",
    "</body>\n",
    "</html>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 加载本地Python文件\n",
    "\n",
    "**使用场景**\n",
    "想在Jupyter Notebook中加载本地的Python文件并执行文件代码。\n",
    "\n",
    "**方法**\n",
    "执行以下命令：\n",
    "```\n",
    "%load Python文件的绝对路径\n",
    "```\n",
    "\n",
    "**注意**\n",
    "+ Python文件的后缀为`.py`。\n",
    "+ `%load`后跟的是Python文件的绝对路径。\n",
    "+ 输入命令后，可以按`CTRL`回车来执行命令。第一次执行，是将本地的Python文件内容加载到单元格内。此时，Jupyter Notebook会自动将`%load`命令注释掉（即在前边加井号“#”），以便在执行已加载的文件代码时不重复执行该命令；第二次执行，则是执行已加载文件的代码。\n",
    "\n",
    "**例**\n",
    "\n",
    "![jupyter-notebook-加载本地Python文件](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E5%8A%A0%E8%BD%BD%E6%9C%AC%E5%9C%B0Python%E6%96%87%E4%BB%B6_20191216212956.webp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "UsageError: Missing filename, URL, input history range, macro, or element in the user namespace.\n"
     ]
    }
   ],
   "source": [
    "%load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 直接运行本地Python文件\n",
    "\n",
    "**使用场景**\n",
    "不想在Jupyter Notebook的单元格中加载本地Python文件，想要直接运行。\n",
    "\n",
    "**方法**\n",
    "执行命令：\n",
    "```\n",
    "%run Python文件的绝对路径\n",
    "```\n",
    "\n",
    "或\n",
    "```\n",
    "!python Python文件的绝对路径\n",
    "```\n",
    "\n",
    "**注意**\n",
    "+ Python文件的后缀为`.py`。\n",
    "+ `%run`后跟的是 Python 文件的绝对路径。\n",
    "+ `!python`用于执行 Python 的代码。\n",
    "+ `!python`属于 !shell命令 语法的使用，即在Jupyter Notebook中执行shell命令的语法。\n",
    "+ 输入命令后，可以按 `control+return（运行本单元，不跳转下一个单元）` 来执行命令，执行过程中将不显示本地Python文件的内容，直接显示运行结果。\n",
    "\n",
    "**例**\n",
    "![jupyter-notebook-运行本地Python文件](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E8%BF%90%E8%A1%8C%E6%9C%AC%E5%9C%B0Python%E6%96%87%E4%BB%B6_20191216215604.webp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.7.5 (default, Oct 25 2019, 10:52:18) \r\n",
      "[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin\r\n",
      "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\r\n",
      ">>> "
     ]
    }
   ],
   "source": [
    "!python "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在Jupyter Notebook中获取当前位置\n",
    "\n",
    "**使用场景**\n",
    "想要在Jupyter Notebook中获取当前所在位置的绝对路径。\n",
    "\n",
    "**方法**\n",
    "```\n",
    "%pwd\n",
    "```\n",
    "或\n",
    "```\n",
    "!pwd\n",
    "```\n",
    "**注意**\n",
    "获取的位置是当前 Jupyter Notebook 中创建的笔记本所在位置，且该位置为绝对路径。\n",
    "`!pwd`属于 !shell 命令语法的使用，即在 Jupyter\n",
    "Notebook 中执行 shell 命令的语法。\n",
    "\n",
    "**例**\n",
    "![jupyter-notebook-获取当前位置的绝对路径](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E4%BD%8D%E7%BD%AE%E7%9A%84%E7%BB%9D%E5%AF%B9%E8%B7%AF%E5%BE%84_20191216215943.webp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/Users/davidfnck/Downloads/All-Lessons/Python Tutorial for Humans™/002-安装 Python'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%pwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 在Jupyter Notebook使用shell命令\n",
    "### 方法一：在笔记本的单元格中**\n",
    "\n",
    "**语法**\n",
    "```\n",
    "!shell命令\n",
    "```\n",
    "\n",
    "在 Jupyter Notebook 中的笔记本单元格中用英文感叹号`!`后接 shell 命令即可执行 shell 命令。\n",
    "\n",
    "**例**\n",
    "![jupyter-notebook-Shell命令的使用](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-Shell%E5%91%BD%E4%BB%A4%E7%9A%84%E4%BD%BF%E7%94%A8_20191216220910.webp)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: requests in /Users/davidfnck/anaconda3/lib/python3.7/site-packages (2.22.0)\r\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/davidfnck/anaconda3/lib/python3.7/site-packages (from requests) (2019.11.28)\r\n",
      "Requirement already satisfied: idna<2.9,>=2.5 in /Users/davidfnck/anaconda3/lib/python3.7/site-packages (from requests) (2.8)\r\n",
      "Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/davidfnck/anaconda3/lib/python3.7/site-packages (from requests) (3.0.4)\r\n",
      "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/davidfnck/anaconda3/lib/python3.7/site-packages (from requests) (1.24.2)\r\n"
     ]
    }
   ],
   "source": [
    "!pip install requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "### 方法二：在Jupyter Notebook中新建终端\n",
    "\n",
    "**启动方法**\n",
    "在 Jupyter Notebook 主界面，即`File`界面中点击`New`；在`New`下拉框中点击`Terminal`即新建了终端。此时终端位置是在你的`Home`目录，可以通过`pwd`命令查询当前所在位置的绝对路径。\n",
    "\n",
    "**关闭方法**\n",
    "在Jupyter Notebook的“Running”界面中的“Terminals”类目中可以看到正在运行的终端，点击后边的“Shutdown”即可关闭终端。\n",
    "\n",
    "**例**\n",
    "![jupyter-notebook-笔记本中的终端使用](https://pptwinpics.oss-cn-beijing.aliyuncs.com/jupyter-notebook-%E7%AC%94%E8%AE%B0%E6%9C%AC%E4%B8%AD%E7%9A%84%E7%BB%88%E7%AB%AF%E4%BD%BF%E7%94%A8_20191216221259.webp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pptwinpics.oss-cn-beijing.aliyuncs.com/CDA%E8%AE%B2%E5%B8%88%E6%B0%B4%E5%8D%B0_20200314161940.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "250.341px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
